// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import ComponentBase from "../../../../general/Script/component/ComponentBase";
import DataMgr from "../../../../general/Script/mgr/dataManager";


const {ccclass, property} = cc._decorator;

@ccclass
export default class BlockTouch extends ComponentBase {

    
    //方块颜色
    blockType:number = 0;

    startPos:cc.Vec2 = null;

    // LIFE-CYCLE CALLBACKS:
    startNode:cc.Node = null

    startScale:number = 0.4;
    
    touchIndex:number = 0;

    diamondInfo:any = [];
    onLoad () {
        super.onLoad();
    }

    
    start () {

    }

    init(type,index:number,diamondArr:string[] = []){
        const spArr = DataMgr.Ins().getGameData('blockSp');
       
        for(let i  = 0; i<this.node.children.length;i++){
            const block = this.node.children[i];
            block.getComponent(cc.Sprite).spriteFrame = spArr[type];
        }
        this.node.scale = 0;
        this.startPos = this.node.getPosition();
        this.blockType = type;
        this.node.runAction(cc.scaleTo(0.1,0.4).easing(cc.easeIn(2)));
        this.touchIndex = index;
        console.log('diamondArr = ',diamondArr);
        if(diamondArr.length>0){
            this.initDiamond(diamondArr);   
        }
      
    }

    initDiamond(diamondArr:string[] = []){
        //['0_0', '5_0', '2_1']
        const activeNodes = [];
        for(let i in this.node.children){
            const child = this.node.children[i]
            if(child.opacity == 255){
                activeNodes.push(child)
            }
        }


        for(let i in diamondArr){
            const indexArr = diamondArr[i].split('_');
            const index = Number(indexArr[0]) 
            const type =  Number(indexArr[1]);
            if(activeNodes[index]){
                this.setDiamondType(activeNodes[index],Number(type));
            }
        }

    }

    setDiamondType(node:cc.Node,type){
        const diamondSpArr = DataMgr.Ins().getGameData('diamondsSp');
        console.log('node.type  = ',node.name,type );
        const diamondSp =  DataMgr.Ins().getGameData('blockDiamondSp');
        node.getComponent(cc.Sprite).spriteFrame = diamondSp;
        if(node.childrenCount > 0){
            node.children[0].getComponent(cc.Sprite).spriteFrame = diamondSpArr[type];
            node.children[0].active = true;
        }else{
            const diamond = new cc.Node();
            diamond.addComponent(cc.Sprite).spriteFrame = diamondSpArr[type];
            diamond.parent = node;
            diamond.active = true;
        }


        let rowcol = [];
        if(node.name === this.startNode.name){
            rowcol = [0,0]
        }else{
            rowcol = node.name.split('_');
        }
        console.log('touchNode = ',rowcol);
        console.log('diamondInfo = ',this.diamondInfo);
        const diamondInfo =`${rowcol[0]}_${rowcol[1]}_${type}` 
        
        
        
        this.diamondInfo.push(diamondInfo);

    }


    getTouchIndex(){
        return this.touchIndex;
    }

    getBlockType(){
        return this.blockType;
    }

    resetPos(pos:cc.Vec2){
        const newPos= cc.v2(this.node.position.x + pos.x,this.node.position.y + pos.y);
        this.node.setPosition(newPos);
    }

    touch(){
        this.node.runAction(cc.scaleTo(0.1,1));
        // this.node.scale = 1;
        this.node.y+= 200;
        this.node.zIndex = 10;
    }

    back(){
        this.node.runAction(cc.scaleTo(0.1,this.startScale));
        this.node.runAction(cc.moveTo(0.1,this.startPos));
        this.node.zIndex = 1;
    }
    

    getWorldPos(){
        const pos_world = this.node.convertToWorldSpaceAR(this.startNode.getPosition())
        return pos_world
    }


    getBlockIndexArr(){
        const arr = []
        if(this.startNode.opacity == 255){
            arr.push([0,0])
        }
        
        for(let i in this.node.children){
            const child = this.node.children[i]
            if(child.name == this.startNode.name){
                continue;
            }
            if(child.opacity == 255){
                const name = child.name;
                const indexArr = name.split('_');
                if(indexArr.length == 2){
                    arr.push([Number(indexArr[0]),Number(indexArr[1])])
                }
            }
        }
        return arr
    }


    getDiamondArr(){
        return this.diamondInfo;
    }

    canNotTouch(){
        for(let i in this.node.children){
            this.node.children[i].color = cc.color(120,120,120);
        }
    }


    canTouch(){
        const spArr = DataMgr.Ins().getGameData('blockSp');
        for(let i in this.node.children){
            this.node.children[i].getComponent(cc.Sprite).spriteFrame = spArr[this.blockType];
        }
    }



    getActiveLength(){
        let count = 0;
        for(let i in this.node.children){
            if(this.node.children[i].opacity == 255){
                count++;
            }
        }
        return count;
    }

    onDestroy(): void {
        super.onDestroy();
        console.log('block touch destroy')
    }

    // update (dt) {}
}
